package MyLinkList_;

import java.util.List;

/*
 *@Student 张策
 */
public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public void createList(){
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode1.next=listNode2;
        listNode2.next=listNode3;
        listNode3.next=listNode4;
        this.head=listNode1;
    }
    @Override
    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);
        listNode.next=this.head;
        this.head=listNode;
    }

    @Override
    public void addLast(int data) {
        ListNode node=new ListNode(data);
        if (head==null){
            head=node;
        }else {
            ListNode cur;
             cur=head;
            while (cur.next!=null){
                cur=cur.next;
            }
            cur.next=node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        ListNode cur=head;
        int num=1;
        while (cur.next!=null){
            if(num==index-1){
                node.next=cur.next;
                cur.next=node;
                break;
            }
            num++;
            cur=cur.next;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
              return   true;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while (cur.next!=null){
            if(cur.next.val==key){
                cur.next=cur.next.next;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head.next;
        ListNode pre=head;
        while (cur!=null){
            if(cur.val==key){
                pre.next=cur.next;
            }else {
                pre=cur;
            }
            cur=cur.next;
        }
        if(head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        int num=0;
        ListNode cur=head;
        while (cur!=null){
            num++;
            cur=cur.next;
        }
        return num;
    }

    @Override
    public void clear() {
        head=null;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while (cur!=null){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }
    public void fan(){
        ListNode cur=head.next;
        head.next=null;
        while (cur!=null) {
            ListNode curNext=cur.next;
            cur.next=head;
            head=cur;
            cur=curNext;
        }
    }
    public ListNode Mid(){
        ListNode fast=head;
        ListNode last=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            last=last.next;
        }
        return last;
    }
}
